#!/bin/bash
# Copyright (C) 2011 all rights reserved 
# GNU GENERAL PUBLIC LICENSE VERSION 3.0
# Author bjarneh@ifi.uio.no

D=`dirname "$0"`
B=`basename "$0"`
FULL="`cd \"$D\" 2>/dev/null && pwd || echo \"$D\"`/$B"
HERE=$(dirname "$FULL")
TARGET=""
BASETARGET=""

function init() {
    cd "$HERE"
    TARGET=$(ls ../target/cop-*.jar 2>/dev/null)
    if [ -z "$TARGET" ];then
        build
    fi
    TARGET=$(ls ../target/cop-*.jar)
    BASETARGET=$(basename "$TARGET")
}

function build(){
    pathfind 'mvn'       || die "[ERROR] missing 'mvn (maven2)'"
    echo -n "Missing target will build "
    cd ..
    mvn package 2>/dev/null 1>/dev/null || die " => Failed to build cop"
    cd "$HERE"
    echo "...done"
}

function phelp(){
cat <<EOH

build.sh - utility script for cop

targets: 

  help    : print this menu and exit
  install : install cop in /opt/cop/* /usr/bin/cop (as root)
  clean   : == rm -rf /opt/cop /usr/bin/cop        (as root)
  debian  : build a debian package => cop_0.2-0_all.deb

EOH
}

function die(){
    echo "$1"
    exit 1
}

# Make sure we have all binaries needed in order to build debian package
function sanity(){
    pathfind 'hg'       || die "[ERROR] missing 'hg (mercurial)'"
    pathfind 'gzip'     || die "[ERROR] missing 'gzip'"
    pathfind 'md5sum'   || die "[ERROR] missing 'md5sum'"
    pathfind 'dpkg-deb' || die "[ERROR] missing 'dpkg-deb'"
    pathfind 'fakeroot' || die "[ERROR] missing 'fakeroot'"
    # Not too many systems lacking these (coreutils) but still
    pathfind 'cp'       || die "[ERROR] missing 'cp'"
    pathfind 'mkdir'    || die "[ERROR] missing 'mkdir'"
    pathfind 'mv'       || die "[ERROR] missing 'mv'"
    pathfind 'du'       || die "[ERROR] missing 'du'"
    pathfind 'chmod'    || die "[ERROR] missing 'chmod'"
    pathfind 'printf'   || die "[ERROR] missing 'printf'"
}

# Taken from Debian Developers Reference Chapter 6
function pathfind(){
     OLDIFS="$IFS"
     IFS=:
     for p in $PATH; do
         if [ -x "$p/$*" ]; then
             IFS="$OLDIFS"
             return 0
         fi
     done
     IFS="$OLDIFS"
     return 1
}

# create a debian package of cop
function debian() {

# do you have what it takes to build a deb?

    sanity

DEBCONTROL="Package: cop
Version: 0.2
Section: devel
Priority: optional
Architecture: all
Depends: default-jre|java-runtime, bash (>= 3.0)
Suggests: maven
Conflicts:
Replaces:
Installed-Size: %d
Maintainer: Bjarne Holen <bjarneh@ifi.uio.no>
Description: Theorem prover for coherent logic.
 The input format is taken from golog by J. Fisher
 and M. Bezem, and the procedure is based on refutation
 so input formulas should be negated theorems, i.e.
 contradictions. Cop implements a modified version of
 the RETE algorithm for its matching.
 For more info see: http://cop.googlecode.com
"

DEBCOPYRIGHT="Name: cop
Maintainer: bjarne holen <bjarneh@ifi.uio.no>
Source: https://cop.googlecode.com/hg

Files: *
Copyright: 2011, bjarne holen <bjarneh@ifi.uio.no>
License: GPL-3

License: GPL-3
On Debian systems, the complete text of the GNU General Public License
version 3 can be found in '/usr/share/common-licenses/GPL-3'.
"

DEBCHANGELOG="cop (0.2.0) devel; urgency=low

    * The actual changelog can be found in changelog...

 -- Bjarne Holen <bjarneh@ifi.uio.no>  Thu, 25 May 2011 14:07:28 -0400
"

BASHSTUB="#!/bin/bash
java -jar /usr/share/cop/$BASETARGET \$@
"

    mkdir -p ./debian/DEBIAN
    mkdir -p ./debian/usr/bin
    mkdir -p ./debian/usr/share/cop
    mkdir -p ./debian/usr/share/man/man1
    mkdir -p ./debian/usr/share/doc/cop
    mkdir -p ./debian/etc/bash_completion.d

    chmod -R 755 ./debian

    cp "$TARGET" ./debian/usr/share/cop/
    echo "$BASHSTUB" > ./debian/usr/bin/cop

    cp ./cop-completion.sh ./debian/etc/bash_completion.d/cop
    cat ./cop.1 | gzip --best - > ./debian/usr/share/man/man1/cop.1.gz
    echo "$DEBCOPYRIGHT" > ./debian/usr/share/doc/cop/copyright

    hg log | gzip --best - > ./debian/usr/share/doc/cop/changelog.gz
    echo "$DEBCHANGELOG" | gzip --best - > ./debian/usr/share/doc/cop/changelog.Debian.gz
    arr=($(du -s ./debian))
    printf "$DEBCONTROL" "${arr[0]}" > ./debian/DEBIAN/control
    echo "/etc/bash_completion.d/cop" > ./debian/DEBIAN/conffiles

    chmod 755 ./debian/usr/bin/cop
    chmod 755 ./debian/etc/bash_completion.d/cop
    chmod 644 ./debian/DEBIAN/conffiles
    chmod 644 ./debian/usr/share/cop/cop-0.2.jar
    chmod 644 ./debian/usr/share/doc/cop/changelog.Debian.gz
    chmod 644 ./debian/usr/share/doc/cop/changelog.gz
    chmod 644 ./debian/usr/share/doc/cop/copyright
    chmod 644 ./debian/usr/share/man/man1/cop.1.gz

    fakeroot dpkg-deb --build ./debian
    mv debian.deb "cop_0.2-0_all.deb"
    rm -rf ./debian

}

function plainInstall() {
    [ -d '/opt/cop' ] || mkdir -p /opt/cop/bin || slutt ""
    echo "cp $TARGET /opt/cop/bin"
    cp "$TARGET" /opt/cop/bin || die "Failed to copy /opt/cop/bin/$TARGET"
    echo "creating file: /usr/bin/cop"
    echo -e "#!/bin/bash\njava -jar /opt/cop/bin/${BASETARGET} \$@" > /usr/bin/cop || die "Failed to write /opt/bin/cop"
    chmod a+rx /usr/bin/cop
}

function plainRemove() {

    if [ -d "/opt/cop" ]; then
        echo "rm -rf /opt/cop"
        rm -rf "/opt/cop"
    fi

    if [ -f "/usr/bin/cop" ]; then
        echo "rm /usr/bin/cop"
        rm "/usr/bin/cop"
    fi

}

# main
{

case "$1" in
     'help' | '-h' | '--help' | '-help')
      phelp
      ;;
      'install' | '--install' | '-install')
      init
      plainInstall
      ;;
      '-c' | 'clean' | '--clean' | '-clean')
      init
      plainRemove
      ;;
      '-d' |'debian' | '--debian' | '-debian')
      init
      debian
      ;;
      *)
      phelp
      ;;
esac
}
